package engine.systems.timeline.timelineBitmap
{
	import engine.classes.display.BitmapCreator;
	import engine.classes.enumerations.SystemPriority;
	import engine.systems.update.GameSystem;
	import engine.utils.BitmapUtils;
	import engine.utils.DisplayUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	public class TimelineBitmapSystem extends GameSystem
	{
		public function TimelineBitmapSystem()
		{
			super(TimelineBitmapNode, updateNode, nodeAdded, nodeRemoved);
			
			this.priority = SystemPriority.POST_TIMELINE;
		}
		
		private function updateNode(node:TimelineBitmapNode, time:Number):void
		{
			if(node.timeline.locked) return;
			
			var frame:int = node.timeline.frame;
			if(frame == node.bitmap._frame) return;
			
			node.bitmap._frame = frame;
			
			var data:TimelineBitmapData = node.bitmap._data[frame];
			if(data)
			{
				var bitmap:Bitmap 	= node.bitmap._bitmap;
				bitmap.x 			= data.offsetX;
				bitmap.y 			= data.offsetY;
				
				if(data.data) bitmap.bitmapData = data.data;
			}
		}
		
		private function nodeAdded(node:TimelineBitmapNode):void
		{
			var clip:MovieClip = node.render.display as MovieClip;
			var timelineBitmap:TimelineBitmap = node.bitmap;
			
			node.timeline.parseClip(clip);
			
			var creator:BitmapCreator 		= new BitmapCreator(clip, timelineBitmap.quality);
			var previousBounds:Rectangle 	= creator.bounds;
			var previousData:BitmapData		= creator.createBitmapData();
			
			var sprite:Sprite 	= new Sprite();
			sprite.mouseEnabled = false;
			sprite.transform 	= clip.transform;
			sprite.name 		= clip.name;
			
			var bitmap:Bitmap 		= new Bitmap(previousData);
			timelineBitmap._bitmap 	= bitmap;
			bitmap.x				= previousBounds.left;
			bitmap.y				= previousBounds.top;
			bitmap.scaleX 			= 1 / timelineBitmap._quality;
			bitmap.scaleY 			= 1 / timelineBitmap._quality;
			sprite.addChild(bitmap);
			
			var timelineBitmapData:TimelineBitmapData = new TimelineBitmapData();
			timelineBitmapData.offsetX 	= previousBounds.left;
			timelineBitmapData.offsetY 	= previousBounds.top;
			timelineBitmapData.data		= previousData;
			timelineBitmap._data[1] 	= timelineBitmapData;
			
			var totalFrames:int = clip.totalFrames;
			for(var i:int = 2; i <= totalFrames; i++)
			{
				clip.gotoAndStop(i);
				creator.refreshBounds();
				
				var currentBounds:Rectangle = creator.bounds;
				var currentData:BitmapData 	= creator.createBitmapData();
				
				if(!BitmapUtils.equalBitmapData(previousData, currentData))
				{
					trace("Different Data");
					previousData = currentData;
					
					timelineBitmapData = new TimelineBitmapData();
					timelineBitmapData.offsetX 	= currentBounds.left;
					timelineBitmapData.offsetY 	= currentBounds.top;
					timelineBitmapData.data		= currentData;
					timelineBitmap._data[i]		= timelineBitmapData;
				}
				else if(previousBounds.left != currentBounds.left || previousBounds.top != currentBounds.top)
				{
					trace("Different Bounds");
					previousBounds = currentBounds;
					
					timelineBitmapData = new TimelineBitmapData();
					timelineBitmapData.offsetX 	= currentBounds.left;
					timelineBitmapData.offsetY 	= currentBounds.top;
					timelineBitmap._data[i]		= timelineBitmapData;
				}
			}
			
			DisplayUtils.swapChild(clip, sprite);
			node.render.display = sprite;
		}
		
		private function nodeRemoved(node:TimelineBitmapNode):void
		{
			
		}
	}
}